package com.design.softwaredesign.service.impl;

import com.aliyun.oss.internal.OSSUtils;
import com.design.softwaredesign.entity.Good;
import com.design.softwaredesign.entity.GoodImages;
import com.design.softwaredesign.entity.TransactionLog;
import com.design.softwaredesign.front.GoodInfo;
import com.design.softwaredesign.mapper.BackCampusMapper;
import com.design.softwaredesign.service.BackCampusService;
import com.design.softwaredesign.utils.HttpClientUtil;
import com.design.softwaredesign.utils.JsonUtils;
import com.design.softwaredesign.utils.OssUtil;
import com.design.softwaredesign.vo.DataVo;
import com.design.softwaredesign.vo.GoodVo;
import com.design.softwaredesign.vo.TimeStampVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * @author :WaiterXiaoYY
 * @description: TODO
 * @data :2020/11/4 23:47
 */
@Service
public class BackCampusServiceImpl implements BackCampusService {

    @Autowired
    private BackCampusMapper backCampusMapper;

    @Override
    public DataVo getAllCampus(Integer pageNum, Integer pageSize, String query, String goodState) {
        DataVo dataVo = new DataVo();
        dataVo.setCode(202);
        dataVo.setMsg("获取所有发布失败");
        try {
            PageHelper.startPage(pageNum, pageSize);
            List<Good> goodList = backCampusMapper.getAllCampus(query, goodState);
            PageInfo<Good> goodPageInfo = new PageInfo<>(goodList);
            List<GoodVo> goodVoList = new ArrayList<>();
            Good good = null;
            for(int i = 0; i < goodList.size(); i++) {
                GoodVo goodVo = new GoodVo();
                good = goodList.get(i);
                BeanUtils.copyProperties(good, goodVo);
                if(!good.getImageUrl().equals("")) {
                    String []imageUrls = good.getImageUrl().split(";");
                    List<String> imageUrlList = new ArrayList<>();
                    for(String imageUrl : imageUrls) {
                        imageUrlList.add(imageUrl);
                    }
                    goodVo.setImageUrlList(imageUrlList);
                }
                goodVoList.add(goodVo);
            }
            dataVo.setCode(200);
            dataVo.setMsg("获取所有发布成功");
            dataVo.setData(goodVoList);
            dataVo.setCount(goodPageInfo.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return dataVo;
        }

    }

    @Override
    public DataVo deleteCampusImage(String goodId, Integer index) {
        DataVo dataVo = new DataVo();
        try {
            GoodImages goodImages = backCampusMapper.getGoodImages(goodId);
            if(!goodImages.getImageUrl().equals("")) {
                String imageurl[] = goodImages.getImageUrl().split(";");
                List<String> newImageUrl = new LinkedList<>();
                String newImageStr = "";
                HashMap<String, List<String>> fileList = new HashMap<>();
                for (int i = 0; i < imageurl.length; i++) {
                    if(index != i) {
                        newImageUrl.add(imageurl[i]);
                    }
                    if(index == i) {
                        OssUtil ossUtil = new OssUtil();
                        String imageName = imageurl[i].split("/")[4];
                        ossUtil.deleteImage(imageName);
                    }
                }
                for(int i = 0; i < newImageUrl.size(); i++) {
                    newImageStr = newImageStr + newImageUrl.get(i) + ";";
                }
                goodImages.setImageUrl(newImageStr);
            }
            backCampusMapper.updateCampusImage(goodImages);
            dataVo.setCode(200);
            dataVo.setMsg("删除图片成功");
        } catch (Exception e) {
            dataVo.setCode(202);
            dataVo.setMsg("删除图片失败");
            e.printStackTrace();
        } finally {
            return dataVo;
        }
    }

    @Override
    public DataVo updateCampusInfo(GoodInfo goodInfo) {
        DataVo dataVo = new DataVo();
        dataVo.setCode(202);
        dataVo.setMsg("更新闲置信息失败");
        try {
            if(backCampusMapper.updateCampusInfo(goodInfo) == 1) {
                dataVo.setCode(200);
                dataVo.setMsg("更新闲置信息成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return dataVo;
        }
    }

    @Override
    public DataVo deleteCampus(String goodId) {
        DataVo dataVo = new DataVo();
        dataVo.setCode(202);
        dataVo.setMsg("删除闲置失败");
        try {
            backCampusMapper.deleteGoodImagesByGoodId(goodId);
            backCampusMapper.deleteGoodDataByGoodId(goodId);
            backCampusMapper.deleteGoodByGoodId(goodId);
            dataVo.setCode(200);
            dataVo.setMsg("删除闲置成功");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return dataVo;
        }
    }

    @Override
    public DataVo getCampusTimeStamp(String goodId) {
        DataVo dataVo = new DataVo();
        dataVo.setCode(202);
        dataVo.setMsg("获取时间线失败");
        try {
            List<TimeStampVo> timeStampVoList = new ArrayList<>();
            TimeStampVo timeStampVo = new TimeStampVo();
            Good good = backCampusMapper.selectGoodByGoodId(goodId);
            timeStampVo.setContent("闲置商品发布成功");
            timeStampVo.setTimeStamp(good.getPostTime());
            timeStampVoList.add(timeStampVo);
            TransactionLog transactionLog = backCampusMapper.selectTransactionLog(goodId);
            if(transactionLog != null) {
                TimeStampVo timeStampVo1 = new TimeStampVo();
                timeStampVo1.setContent("交易成功");
                timeStampVo1.setTimeStamp(transactionLog.getTransactionTime());
                timeStampVoList.add(timeStampVo1);
            }
            dataVo.setCode(200);
            dataVo.setMsg("获取时间线成功");
            dataVo.setData(timeStampVoList);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return dataVo;
        }
    }
}
